<%--
  Created by IntelliJ IDEA.
  User: 86158
  Date: 2022/10/25
  Time: 11:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>HRM</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>

<div class="layui-card layui-content">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="component-form-element">
            <input type="hidden" id="id" name="id" value="${emp.id}">
            <div class="layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">员工名称</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="name"
                               value="${emp.name}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg6">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <select name="sex" id="sex">
                            <option
                                    <c:if test="${emp.sex == 0}">selected</c:if> value="0">女
                            </option>
                            <option
                                    <c:if test="${emp.sex == 1}">selected</c:if> value="1">男
                            </option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block">
                        <select name="deptId" id="deptId">
                            <%--<option value="">------ 请输入-----</option>--%>
                            <c:forEach var="dept" items="${depts}">
                                <option
                                        <c:if test="${emp.deptId == dept.id}">selected</c:if> value="${dept.id}">
                                        ${dept.name}
                                </option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="layui-col-lg6">
                    <label class="layui-form-label">职位</label>
                    <div class="layui-input-block">
                        <select name="jobId" id="jobId">
                            <%--<option value="">------ 请输入-----</option>--%>
                            <c:forEach var="job" items="${jobs}">
                                <option
                                        <c:if test="${emp.jobId == job.id}">selected</c:if> value="${job.id}">
                                        ${job.name}
                                </option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">手机号码</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="phone"
                               value="${emp.phone}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg6">
                    <label class="layui-form-label">电话号码</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="tel"
                               value="${emp.tel}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="email"
                               value="${emp.email}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg6">
                    <label class="layui-form-label">QQ</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="qq"
                               value="${emp.qq}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">学历</label>
                    <div class="layui-input-block">
<%--                        <input type="text"--%>
<%--                               name="education"--%>
<%--                               value="${emp.education}"--%>
<%--                               autocomplete="off"--%>
<%--                               class="layui-input">--%>
                        <select name="education" id="education">
                            <option <c:if test="${emp.education == '高中'}">selected</c:if>>高中</option>
                            <option <c:if test="${emp.education == '专科'}">selected</c:if>>专科</option>
                            <option <c:if test="${emp.education == '本科'}">selected</c:if>>本科</option>
                            <option <c:if test="${emp.education == '硕士'}">selected</c:if>>硕士</option>
                            <option <c:if test="${emp.education == '博士'}">selected</c:if>>博士</option>
                            <option <c:if test="${emp.education == '其他'}">selected</c:if>>其他</option>
                        </select>
                    </div>
                </div>
                <div class="layui-col-lg6">
                    <label class="layui-form-label">专业</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="speciality"
                               value="${emp.speciality}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">身份证号码</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="cardId"
                               value="${emp.cardId}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg6">
                    <label class="layui-form-label">政治面貌</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="party"
                               value="${emp.party}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">民族</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="race"
                               value="${emp.race}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg6">
                    <label class="layui-form-label">联系地址</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="address"
                               value="${emp.address}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">生日</label>
                    <div class="layui-input-block">
                        <input type="text"
                               id="birthday"
                               name="birthday"
                               value="${emp.birthday}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg6">
                    <label class="layui-form-label">爱好</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="hobby"
                               value="${emp.hobby}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">邮政编码</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="postCode"
                               value="${emp.postCode}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-col-lg6">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <input type="text"
                               name="remark"
                               value="${emp.remark}"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <br>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="Add-filter">修改</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    layui.use(['form', 'layer', 'jquery', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery,
            laydate = layui.laydate;

        laydate.render({
            elem: '#birthday', //指定元素
        });

        form.on('submit(Add-filter)', function (data) {
            var field = data.field;
            //数据校验
            if (true) {
                var nameReg = /^[\u4e00-\u9fa5]{2,4}$/;
                var phoneReg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
                var telReg = /^(0\d{2,3})-?(\d{7,8})$/;
                var emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
                var qqReg = /^[1-9][0-9]\d{4,9}$/;
                var postCodeReg = /^[1-9]\d{5}$/;
                var cardIdReg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
                if (field.name.trim() == "") {
                    layer.msg("姓名不能为空", {icon: 2});
                    return false;
                }else if(!nameReg.test(field.name.trim())){
                    layer.msg("姓名只能输入2-4个中文", {icon: 2});
                    return false;
                }
                if (field.sex.trim() == "") {
                    layer.msg("性别不能为空", {icon: 2});
                    return false;
                }
                if (field.deptId.trim() == "") {
                    layer.msg("部门不能为空", {icon: 2});
                    return false;
                }
                if (field.jobId.trim() == "") {
                    layer.msg("职位不能为空", {icon: 2});
                    return false;
                }
                if (field.phone.trim() == "") {
                    layer.msg("手机号码不能为空", {icon: 2});
                    return false;
                }else if(!phoneReg.test(field.phone.trim())){
                    layer.msg("手机号码不合法，请重新输入", {icon: 2});
                    return false;
                }
                if (field.tel.trim() == "") {
                    layer.msg("电话号码不能为空", {icon: 2});
                    return false;
                }else if(!telReg.test(field.tel.trim())){
                    layer.msg("电话号码不合法，请重新输入", {icon: 2});
                    return false;
                }
                if (field.email.trim() == "") {
                    layer.msg("邮箱不能为空", {icon: 2});
                    return false;
                }else if(!emailReg.test(field.email.trim())){
                    layer.msg("邮箱不合法，请重新输入", {icon: 2});
                    return false;
                }
                if (field.qq.trim() == "") {
                    layer.msg("QQ不能为空", {icon: 2});
                    return false;
                }else if(!qqReg.test(field.qq.trim())){
                    layer.msg("QQ账号不合法，请重新输入", {icon: 2});
                    return false;
                }
                if (field.education.trim() == "") {
                    layer.msg("学历不能为空", {icon: 2});
                    return false;
                }
                if (field.speciality.trim() == "") {
                    layer.msg("专业不能为空", {icon: 2});
                    return false;
                }
                if (field.cardId.trim() == "") {
                    layer.msg("身份证号码不能为空", {icon: 2});
                    return false;
                }else if(!cardIdReg.test(field.cardId.trim())){
                    layer.msg("身份证号码不合法，请重新输入", {icon: 2});
                    return false;
                }
                if (field.party.trim() == "") {
                    layer.msg("政治面貌不能为空", {icon: 2});
                    return false;
                }
                if (field.race.trim() == "") {
                    layer.msg("民族不能为空", {icon: 2});
                    return false;
                }
                if (field.address.trim() == "") {
                    layer.msg("联系地址不能为空", {icon: 2});
                    return false;
                }
                if (field.birthday.trim() == "") {
                    layer.msg("生日不能为空", {icon: 2});
                    return false;
                }
                if (field.hobby.trim() == "") {
                    layer.msg("爱好不能为空", {icon: 2});
                    return false;
                }
                if (field.postCode.trim() == "") {
                    layer.msg("邮政编码不能为空", {icon: 2});
                    return false;
                }else if(!postCodeReg.test(field.postCode.trim())){
                    layer.msg("邮政编码不合法，请重新输入", {icon: 2});
                    return false;
                }
            }
            $.ajax({
                url: "${pageContext.request.contextPath}/employee/update",
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(data.field),
                dataType: 'json',
                success: function (data) {
                    if (data == "1") {
                        layer.msg("操作成功", {
                            icon: 1,
                            success: function () {
                                var frameIndex = parent.layer.getFrameIndex("Update-frame");
                                parent.layer.close(frameIndex);
                                var dom = $("#SearchBtn", window.parent.document);
                                dom.trigger("click");
                            }
                        });
                    } else {
                        layer.msg("修改失败", {icon: 2});
                    }
                },
                error: function (e) {
                    layer.msg(e.responseText, {icon: 2});
                }

            })
            return false;
        });
    });

</script>

</body>
</html>
